package chinese.admin.dao;

import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;

import chinese.admin.domain.ChnAuthor;
/**
 * <p>数据访问类</p>
 * <p>Table: ChnAuthor</p>
 *
 */
 @DAO
public interface ChnAuthorDAO {

	@SQL("select t.* from (select (select @i:=@i+1) as rangeNum, r.* FROM (select a.*,COUNT(b.id) as articleCnt,SUM(b.praise) AS popular from ChnAuthor a JOIN ChnArticle b ON a.id=b.authorId and b.deleteFlag=1 and b.status=2 where a.deleteFlag=1 #if(:t.year!=-1){and b.year=:t.year} #if(:t.month!=-1){and b.month=:t.month} #if(:t.yearForWeek!=-1){and b.yearForWeek=:t.yearForWeek} #if(:t.week!=-1){and b.week=:t.week} GROUP BY a.id ORDER BY popular DESC) as r,(select @i:=0) as it) as t #if(:t.author!=''){ where t.author like '%##(:t.author)%'} LIMIT :t.start,:t.length")
	List<ChnAuthor> query(@SQLParam("t")ChnAuthor obj);
	
	@SQL("select COUNT(1) from ChnAuthor a where a.deleteFlag=1 #if(:t.author!=''){ and a.author like '%##(:t.author)%'} and EXISTS (select 1 FROM ChnArticle b where b.deleteFlag=1 and b.authorId=a.id  and b.status=2 #if(:t.year!=-1){and b.year=:t.year} #if(:t.month!=-1){and b.month=:t.month} #if(:t.yearForWeek!=-1){and b.yearForWeek=:t.yearForWeek} #if(:t.week!=-1){and b.week=:t.week})")
	int querySize(@SQLParam("t")ChnAuthor obj);

	@ReturnGeneratedKeys
	@SQL("INSERT INTO ChnAuthor(userId,author,school,grade,deleteFlag,createAt,createUser,updateAt,updateUser) VALUES(:t.userId,:t.author,:t.school,:t.grade,:t.deleteFlag,:t.createAt,:t.createUser,:t.updateAt,:t.updateUser)")
	int create(@SQLParam("t")ChnAuthor obj);
	
	@SQL("UPDATE ChnAuthor SET userId=:t.userId,author=:t.author,school=:t.school,grade=:t.grade,deleteFlag=:t.deleteFlag,createAt=:t.createAt,createUser=:t.createUser,updateAt=:t.updateAt,updateUser=:t.updateUser WHERE id=:t.id")
	void update(@SQLParam("t")ChnAuthor obj);
	
	@SQL("UPDATE ChnAuthor SET deleteFlag=1 WHERE id=:1")
	void delete(int id);

}